package com.cskaoyan.service.mall;

import com.cskaoyan.bean.bo.adminbo.AdminUpdatePasswordBo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketAdminExample;
import com.cskaoyan.bean.po.MarketNoticeAdmin;
import com.cskaoyan.bean.po.MarketNoticeAdminExample;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketNoticeAdminMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProfileServicelmpl implements ProfileService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Autowired
    MarketNoticeAdminMapper marketNoticeAdminMapper;

    @Override
    public int updatePassword(AdminUpdatePasswordBo adminUpdatePasswordBo, Integer id) {
        MarketAdmin marketAdmin = marketAdminMapper.selectByPrimaryKey(id);
        if (!adminUpdatePasswordBo.getOldPassword().equals(marketAdmin.getPassword())) {
            return 3;
        }
        if (adminUpdatePasswordBo.getNewPassword().equals(marketAdmin.getPassword())){
            return 2;
        }
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andIdEqualTo(id);
        MarketAdmin marketAdmin1 = new MarketAdmin();
        marketAdmin1.setUpdateTime(new Date());
        marketAdmin1.setPassword(adminUpdatePasswordBo.getNewPassword());
        int code = marketAdminMapper.updateByExampleSelective(marketAdmin1, marketAdminExample);
        return code;
    }

    @Override
    public CommonData<MarketNoticeAdmin> queryNotice(Integer adminId, BasePageInfo basePageInfo, String title, String type) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketNoticeAdminExample marketNoticeAdminExample = new MarketNoticeAdminExample();
        marketNoticeAdminExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        MarketNoticeAdminExample.Criteria criteria = marketNoticeAdminExample.createCriteria();
        if (!StringUtil.isEmpty(title)) {
            criteria.andNoticeTitleLike("%" + title.trim() + "%");
        }
        if ("unread".equals(type)) {
            criteria.andReadTimeIsNull();
        } else if ("read".equals(type)) {
            criteria.andReadTimeIsNotNull();
        }
        criteria.andAdminIdEqualTo(adminId);
        List<MarketNoticeAdmin> marketNoticeAdmins = marketNoticeAdminMapper.selectByExample(marketNoticeAdminExample);
        PageInfo<MarketNoticeAdmin> marketNoticeAdminPageInfo = new PageInfo<>(marketNoticeAdmins);
        return CommonData.data(marketNoticeAdminPageInfo);
    }
}
